package com.example.a2302fx.fg.minefg

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.PopupWindow
import android.widget.TextView
import androidx.appcompat.app.ActionBar.LayoutParams
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.example.a2302fx.R
import com.example.a2302fx.adapter.ChildAdapter
import com.example.a2302fx.adapter.OrderAdapter
import com.example.a2302fx.databinding.FragmentPayedBinding
import com.example.a2302fx.databinding.FragmentPayingBinding
import com.example.base.entity.Order
import com.example.base.mvvm.BaseFragment

class PayedFragment: BaseFragment<FragmentPayedBinding, OrderViewModel>() {
    lateinit var orderAdapter: OrderAdapter
    override fun initData() {
        orderAdapter = OrderAdapter()
        binding.rv.adapter = orderAdapter
        binding.rv.layoutManager = LinearLayoutManager(context)

        viewModel.order("1",10,1)
        //点击更多，取消订单
        orderAdapter.addChildClickViewIds(R.id.tv_more,R.id.tv_addCar,R.id.tv_refund)
        orderAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.tv_more->{
                    //调用接口
                    viewModel.cancel(orderAdapter.data[position].id)
                    //页面移除
                    orderAdapter.data.removeAt(position)
                    orderAdapter.notifyDataSetChanged()
                }
                R.id.tv_addCar->{
                    viewModel.add(orderAdapter.data[position].orderInfoList[0].cartNum,orderAdapter.data[position].orderInfoList[0].productId.toString(),orderAdapter.data[position].orderInfoList[0].productId)
                }
                R.id.tv_refund->{
                    showWindow(orderAdapter.data[position])
                }
            }
        }
    }

    @SuppressLint("MissingInflatedId")
    private fun showWindow(order: Order) {
        val popupWindow = PopupWindow()
        val view = LayoutInflater.from(context).inflate(R.layout.item_pop, null)
        //关闭图标
        view.findViewById<TextView>(R.id.tv_close).setOnClickListener {
            popupWindow.dismiss()
        }
        //点击切换选中背景
        val refund1 = view.findViewById<TextView>(R.id.refund1)
        val refund2 = view.findViewById<TextView>(R.id.refund2)
        val refund3 = view.findViewById<TextView>(R.id.refund3)
        refund1.setOnClickListener {
            //选中背景
            refund1.setTextColor(Color.RED)
            //refund1.setBackgroundResource()
            //未选中背景
            refund2.setTextColor(Color.BLACK)
            refund3.setTextColor(Color.BLACK)
        }
        refund2.setOnClickListener {
            //选中背景
            refund2.setTextColor(Color.RED)
            //refund1.setBackgroundResource()
            //未选中背景
            refund1.setTextColor(Color.BLACK)
            refund3.setTextColor(Color.BLACK)
        }
        refund3.setOnClickListener {
            //选中背景
            refund3.setTextColor(Color.RED)
            //refund1.setBackgroundResource()
            //未选中背景
            refund1.setTextColor(Color.BLACK)
            refund2.setTextColor(Color.BLACK)
        }
        val childAdapter = ChildAdapter()
        val rv = view.findViewById<RecyclerView>(R.id.refund_rv)
        childAdapter.data.addAll(order.orderInfoList)
        rv.adapter = childAdapter
        rv.layoutManager = LinearLayoutManager(context)

        view.findViewById<Button>(R.id.next).setOnClickListener {
            viewModel.refund(order.id,"","","收货地址填错了",order.orderId)
        }

        popupWindow.width = LayoutParams.MATCH_PARENT
        popupWindow.height = LayoutParams.WRAP_CONTENT
        popupWindow.isOutsideTouchable = true
        popupWindow.contentView = view
        popupWindow.showAtLocation(binding.rv,Gravity.BOTTOM,0,0)
    }

    override fun initView() {
        viewModel.orderSuccess.observe(this){
            if(it.code==200){
                orderAdapter.data.clear()
                orderAdapter.data.addAll(it.data.list)
                orderAdapter.notifyDataSetChanged()
            }
        }
        viewModel.cancelSuccess.observe(this){
            ToastUtils.showLong(it.message)
        }
        viewModel.addSuccess.observe(this){
            ToastUtils.showLong(it.message)
        }
        viewModel.refundSuccess.observe(this){
            ToastUtils.showLong(it.message)
        }
    }

    override fun getViewModel(): Class<OrderViewModel>  = OrderViewModel::class.java

    override fun getLayout(): Int  = R.layout.fragment_payed

}